﻿//+--------------------------------------------------------------------------------------------------
//     
//Description:  ApplicationPayloadViewModel
//Author: Aravind Ramarathinam (aravr@microsoft.com)
//Copyright © Microsoft Corporation
//Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
//You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
//THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
//INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. 
//See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
// 
//--------------------------------------------------------------------------------------------------------------------------------------------------------

using System.ComponentModel;
using System.IO;
using VmroleAuthor.Models;

namespace VmroleAuthor.ViewModels
{
    public class ApplicationPayloadViewModel : ViewModelBase, IDataErrorInfo
    {
        private readonly ApplicationPayload applicationPayload;
        private readonly VmRoleResourceExtensionViewModel vmRoleResourceExtensionViewModel;

        public static class PropertyNames
        {
            public const string Id = "Id";
            public const string RelativePath = "RelativePath";          
        }

        public ApplicationPayloadViewModel(ApplicationPayload applicationPayload, VmRoleResourceExtensionViewModel vmRoleResourceExtensionViewModel)
        {
            this.applicationPayload = applicationPayload;
            this.vmRoleResourceExtensionViewModel = vmRoleResourceExtensionViewModel;
        }

        public ApplicationPayload ApplicationPayload
        {
            get { return this.applicationPayload; }
        }

        public string Id
        {
            get
            {
                return this.applicationPayload.ID;
            }
            set
            {
                this.applicationPayload.ID = value; 
                this.OnPropertyChanged(PropertyNames.Id);
            }
        }

        public string RelativePath
        {
            get
            {
                return this.applicationPayload.RelativePath;
            }
            set
            {
                this.applicationPayload.RelativePath = value;
                this.OnPropertyChanged(PropertyNames.RelativePath);
            }
        }

        public bool IsExternal
        {
            get
            {
                return this.vmRoleResourceExtensionViewModel.ResourceExtensionDataPackageViewModel.ContainsExternalPayloadStartingWithPrefix(this.RelativePath);
            }
        }

        public override string ToString()
        {
            //TODO: Optimize this
            return this.RelativePath + " - " + this.Id;
        }

        public string this[string propertyName]
        {
            get
            {
                string error = null;
                switch (propertyName)
                {
                    case PropertyNames.Id:
                        error = ValidationHelper.ValidateGuid(this.Id);
                        //TODO: Add a check if no one is using/referencing this
                        break;
                    case PropertyNames.RelativePath:
                        error = ValidationHelper.ValidateNotNullOrEmpty(this.RelativePath);
                        if (string.IsNullOrEmpty(error))
                        {
                            //Check if the file or folder exists
                            if (this.vmRoleResourceExtensionViewModel.ResourceExtensionStorageInfo == null)
                            {
                                error = "Resource Extension Storage Info not set";
                            }
                            else if (this.vmRoleResourceExtensionViewModel.ResourceExtensionStorageInfo.WorkingDir == null)
                            {
                                error = "Resource Extension Storage Info working dir not set";
                            }
                            else if (!Directory.Exists(this.vmRoleResourceExtensionViewModel.ResourceExtensionStorageInfo.WorkingDir))
                            {
                                error = string.Format("Resource Extension Storage Info Working dir {0} does not exist", this.vmRoleResourceExtensionViewModel.ResourceExtensionStorageInfo.WorkingDir);
                            }
                            else if (this.vmRoleResourceExtensionViewModel.ResourceExtensionDefinition.DataPackage == null)
                            {
                                error = string.Format("Resource Extension DataPackage is not set");
                            }
                            else if (this.vmRoleResourceExtensionViewModel.ResourceExtensionDefinition.DataPackage.Location == null)
                            {
                                error = string.Format("Resource Extension DataPackage Location is not set");
                            }
                            else
                            {
                                string fullPath = Path.Combine(this.vmRoleResourceExtensionViewModel.ResourceExtensionStorageInfo.WorkingDir, this.vmRoleResourceExtensionViewModel.ResourceExtensionDefinition.DataPackage.Location, this.RelativePath);
                                if (!File.Exists(fullPath) && !Directory.Exists(fullPath) && !this.IsExternal)
                                {
                                    error = string.Format("Resource Extension Application payload {0} does not exist", fullPath);
                                }
                            }
                        }
                        break;
                }
                return error;                 
            }
        }

        public string Error { get;  set; }
    }
}
